package com.aiyige.page.user.behavior;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

import com.aiyige.R;

/**
 * Created by usera on 2018/3/8.
 */

public class ScrollViewBehavior extends CoordinatorLayout.Behavior<View> {
    View dependencyView = null;

    public ScrollViewBehavior() {
    }

    public ScrollViewBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
        layoutParams.topMargin = dependencyView.getMeasuredHeight();
        layoutParams.height = (int) (parent.getMeasuredHeight() - parent.getResources().getDimension(R.dimen.user_page_tab_height) - parent.getResources().getDimension(R.dimen.user_page_title_bar_height));
        return false;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        if (dependency.getId() == R.id.appbar) {
            dependencyView = dependency;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        child.setTranslationY(dependency.getTranslationY());
        return true;
    }
}
